#-------------------------------------------------------------------------------
# By Daoyu @2010-08-13
#-------------------------------------------------------------------------------

from django.shortcuts import render_to_response
from django.http import HttpResponse, HttpResponseRedirect
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
from django.template import RequestContext
from django.contrib.auth.models import User

from django.db.models import Q

from uncle.users.models import *
from uncle.util.page import *
from uncle.util.util import *
from uncle.project.models import *

from django.core.mail import *

class email_stat:
    def __init__(self):
        self.last_es, self.es = None, None

    def set_es(self, _es):
        self.es = _es;

    def set_last_es(self, _last_es):
        self.last_es = _last_es;

#-------------------------------------------------------------------------------
# Show a list of users ordered by last contact date.
# Click them to show the send email page.
#-------------------------------------------------------------------------------
def list(request):
    email_stats = []
    if request.user.has_perm('users.is_artist'):
        for x in Last_email_sta.objects.order_by('time').all():
            y = Email_sta.objects.get(user__id = x.user.id)
            _es = email_stat()
            _es.set_es(y)
            _es.set_last_es(x)
            email_stats.append(_es)

    return render_to_response('user/emails.html',
              {'les':email_stats},
              context_instance = RequestContext(request))

#-------------------------------------------------------------------------------
# Send to an artist his contact email.
#-------------------------------------------------------------------------------
def send(request, u_id):
    if request.method=='GET':
        _es, last_v, last_u = None, 0, 0
        if request.user.has_perm('users.is_artist'):
            x = Last_email_sta.objects.filter(user__id = u_id)
            y = Email_sta.objects.filter(user__id = u_id)
            if x and y:
                _es = email_stat()
                _es.set_es(y[0])
                _es.set_last_es(x[0])
                last_v = y[0].visits - x[0].visits
                last_u = y[0].project_counts - x[0].project_counts
    
        return render_to_response('user/email_send.html',
                  {'x':_es, 'last_visits':last_v,'last_uncle':last_u,},
                  context_instance = RequestContext(request))
    elif request.method=='POST':
        email = EmailMultiAlternatives('Hello from Uncle', 
                                       request.POST['email_content'], 
                                       'noreply@post-database.com',
                                       bcc=[request.POST['cc_address']],
                                       to=[request.POST['to_address']])
    
        email.content_subtype = "html"                  
        try:
            email.send()
            
            if request.user.has_perm('users.is_artist'):
                x = Last_email_sta.objects.filter(user__id = u_id)
                y = Email_sta.objects.filter(user__id = u_id)
                
                if x and y:
                    x[0].visits = y[0].visits
                    x[0].project_counts = y[0].project_counts
                    
                    from datetime import datetime
                    x[0].time = datetime.now()
                    
                    x[0].save()
            
        except:
            request.user.message_set.create(message='Send email encounted problem')
            
        return HttpResponseRedirect('/user/emails/')           
        
            
